package com.punch.system.controller;

import com.punch.common.exception.BusinessException;
import com.punch.common.model.Result;
import com.punch.common.utils.OssUtil;
import com.punch.system.exception.SysExceptionEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * FileController-文件控制层
 *
 * @author wujj
 * @version 1.0.0
 * @since 2020/7/28 10:32
 */
@RestController
@Api(value = "FileController", tags = "文件上传")
public class FileController {


    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/file/upload/{module}")
    @ApiOperation(value = "上传文件", notes = "上传文件", code = 200, produces = "application/json")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file, @PathVariable String module) {
        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            throw new BusinessException(SysExceptionEnum.FILE_DOES_NOT_EXIST);
        }
        return Result.succeed(OssUtil.uploadFile(file, module + "/"), "上传文件成功");
    }
}
